import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export const routes = [
  { 
    path: '/',
    name: 'Index',
    component: () => import('@/views/index'),
    meta: { title: '横山羊肉' },
  },
  {
    path: '/about',
    name: 'AboutUs',
    component: () => import('@/views/about'),
    meta: { title: '关于我们' },
  },
  {
    path: '/product',
    name: 'Product',
    component: () => import('@/views/product'),
    meta: { title: '产品中心' },
  },
  {
    path: '/product/:prodId/detail',
    name: 'ProductDetail',
    component: () => import('@/views/productDetail'),
    meta: { title: '产品中心' },
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import('@/views/shoppingCart'),
    meta: { title: '购物车' },
  },
  {
    path: '/news',
    name: 'News',
    component: () => import('@/views/news'),
    meta: { title: '新闻资讯' },
  },
  {
    path: '/news/:newsId/detail',
    name: 'NewsDetail',
    component: () => import('@/views/newsDetail'),
    meta: { title: '新闻资讯' },
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/register'),
    meta: { title: '注册' },
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login'),
    meta: { title: '登录' },
  },
  {
    path: '/me',
    name: 'Me',
    component: () => import('@/views/user'),
    meta: { title: '个人中心' },
  },
  {
    path: '/feedback',
    name: 'Feedback',
    component: () => import('@/views/feedback'),
    meta: { title: '意见反馈' },
  },
]

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router
